package com.hdjd.controller;

import com.hdjd.entities.School;
import com.hdjd.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘子强
 * 2021/1/28 9:23
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private SchoolService schoolService;

    //分页查询学校集合
    @ResponseBody
    @RequestMapping(value = "/selectSchoolList", method = RequestMethod.GET)
    public Map<String, Object> selectSchoolList(String schProvince, String schName, @RequestParam("page") Integer pageCurrent, @RequestParam("limit") Integer pageSize) {
        Map<String, Object> map = new HashMap<>();

        List<School> schoolList;
        try {
            schoolList = this.schoolService.selectSchoolList(schProvince, schName, pageCurrent, pageSize);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1);
            map.put("msg", "查询失败");
            return map;
        }
        if(!schoolList.isEmpty()){
            map.put("code", 0);
            map.put("msg", "查询成功");
            map.put("count", 21);
            map.put("data", schoolList);
        }else{
            map.put("code", 2);
            map.put("msg", "查询无果");
        }
        return map;
    }
    //根据院校id查询院校信息
    @ResponseBody
    @RequestMapping(value = "/selectSchoolBySchoolId", method = RequestMethod.GET)
    public Map<String, Object> selectSchoolBySchoolId(Integer schoolId) {
        Map<String, Object> map = new HashMap<>();

        School school;
        try {
            school = this.schoolService.selectSchoolBySchoolId(schoolId);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(school != null){
            map.put("code", 1001);
            map.put("msg", "查询成功");
            map.put("school", school);
        }else{
            map.put("code", 1003);
            map.put("msg", "查询无果");
        }
        return map;
    }
    //根据院校id修改院校信息
    @ResponseBody
    @RequestMapping(value = "/updateSchoolBySchoolId", method = RequestMethod.PUT)
    public Map<String, Object> updateSchoolBySchoolId(School school) {
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.schoolService.updateSchoolBySchoolId(school);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "修改失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "修改成功");
        }else{
            map.put("code", 1002);
            map.put("msg", "修改无果");
        }
        return map;
    }
    //新增院校
    @ResponseBody
    @RequestMapping(value = "/addSchool", method = RequestMethod.POST)
    public Map<String, Object> addSchool(School school) {
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.schoolService.insertSchool(school);
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "新增失败");
            return map;
        }
        if(flag){
            map.put("code", 1001);
            map.put("msg", "新增成功");
        }else{
            map.put("code", 1002);
            map.put("msg", "新增失败");
        }
        return map;
    }
    //停用或启用院校（修改院校状态）
    @ResponseBody
    @RequestMapping(value = "/modifySchoolStatus", method = RequestMethod.PUT)
    public Map<String, Object> modifySchoolStatus(Integer schoolId, Integer schStatus) {
        Map<String, Object> map = new HashMap<>();

        boolean flag;
        try {
            flag = this.schoolService.updateSchoolStatusBySchoolId(schoolId, schStatus);
        }catch (Exception e){
            e.printStackTrace();
            if(schStatus == 1){
                map.put("code", 1002);
                map.put("msg", "启用失败");
            }else if (schStatus == 0){
                map.put("code", 1002);
                map.put("msg", "停用失败");
            }
            return map;
        }
        if(flag){
            if(schStatus == 1){
                map.put("code", 1001);
                map.put("msg", "启用成功");
            }else if (schStatus == 0){
                map.put("code", 1001);
                map.put("msg", "停用成功");
            }
        }else{
            if(schStatus == 1){
                map.put("code", 1002);
                map.put("msg", "启用失败");
            }else if (schStatus == 0){
                map.put("code", 1002);
                map.put("msg", "停用失败");
            }
        }
        return map;
    }

    //无分页查询全部院校
    @ResponseBody
    @RequestMapping(value = "/selectAllSchools", method = RequestMethod.GET)
    public Map<String, Object> selectAllSchools() {
        Map<String, Object> map = new HashMap<>();

        List<School> allSchools;
        try {
            allSchools = this.schoolService.selectAllSchools();
        }catch (Exception e){
            e.printStackTrace();
            map.put("code", 1002);
            map.put("msg", "查询失败");
            return map;
        }
        if(!allSchools.isEmpty()){
            map.put("code", 1001);
            map.put("msg", "查询成功");
            map.put("allSchools", allSchools);
        }else{
            map.put("code", 1002);
            map.put("msg", "查询无果");
        }
        return map;
    }


}
